<template>
  <el-dialog :title="!newHouse.houseId ? '新增' : '修改'"
             :close-on-click-modal="false"
             :visible.sync="visible">
    <el-form :model="newHouse"
             :rules="dataRule"
             ref="newHouse"
             @keyup.enter.native="newHouseSubmit()"
             label-width="140px">
      <el-form-item label="金额(/平方)"
                    prop="housePrice">
        <el-input v-model="newHouse.housePrice"
                  placeholder="金额(/平方)"></el-input>
      </el-form-item>
      <el-form-item label="房屋名"
                    prop="houseName">
        <el-input v-model="newHouse.houseName"
                  placeholder="房屋名"></el-input>
      </el-form-item>
      <el-form-item label="地址"
                    prop="houseAddress">
        <el-input v-model="newHouse.houseAddress"
                  placeholder="地址"></el-input>
      </el-form-item>
      <el-form-item label="面积(m2)"
                    prop="houseArea">
        <el-input v-model="newHouse.houseArea"
                  placeholder="面积(m2)"></el-input>
      </el-form-item>
      <!-- <el-form-item label="状态"
                    prop="status">
        <el-input v-model="newHouse.status" placeholder="状态(0.已卖 1.未卖)"></el-input>
        <el-radio-group v-model="newHouse.status">
          <el-radio :label="0">已卖</el-radio>
          <el-radio :label="1">未卖</el-radio>
        </el-radio-group>
      </el-form-item> -->
      <el-form-item label="户型"
                    prop="houseDesc">
        <!-- <el-input v-model="newHouse.houseDesc" placeholder="户型(如1室1厅)"></el-input> -->
        <el-radio-group v-model="newHouse.houseDesc">
          <el-radio-button label="一室一厅"></el-radio-button>
          <el-radio-button label="两室一厅"></el-radio-button>
          <el-radio-button label="三室一厅"></el-radio-button>
          <el-radio-button label="三室两厅"></el-radio-button>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="默认图片"
                    prop="defaultImg">
        <!-- <el-input v-model="newHouse.defaultImg" placeholder="默认图片"></el-input> -->
        <template slot-scope="scope">
          <img :src="newHouse.defaultImg"
               width="100px"
               height="100px"
               alt="">
          <el-upload action="http://localhost:8888/api/house/newhouse/upload"
                     class="upload-demo"
                     :on-success="uploadFile"
                     :on-remove="handleRemove"
                     :file-list="fileList"
                     list-type="picture">
            <el-button size="small"
                       type="primary">点击上传</el-button>
            <div slot="tip"
                 class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
          </el-upload>
        </template>
      </el-form-item>
      <el-form-item label="发布经纪人"
                    prop="userId">
        <el-input v-model="newHouse.userId"
                  placeholder="发布经纪人"></el-input>
      </el-form-item>
      <el-form-item label="标识"
                    prop="flag">
        <!-- <el-input v-model="newHouse.flag" placeholder="标识(1，新房 2.二手房)"></el-input> -->
        <el-radio-group v-model="newHouse.flag">
          <el-radio :label="1">新房</el-radio>
          <el-radio :label="2">二手房</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="创建时间"
                    prop="createTime">
        <!-- <el-input v-model="newHouse.createTime" placeholder="创建时间"></el-input> -->
        <el-date-picker v-model="newHouse.createTime"
                        type="datetime"
                        placeholder="选择日期时间"
                        value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item v-for="(item,index) in modelList"
                    :label="item.group+':'"
                    :key="index">
        <span v-for="(item2,index) in item.params"
              :key="index">
          {{item2.attrName}}<el-input v-model="item2.attrValue"
                    placeholder="请输入内容"></el-input>
          <el-radio-group v-model="item2.searchFlag">
            <el-radio :label="1">可检索</el-radio>
            <el-radio :label="0">不可检索</el-radio>
          </el-radio-group>
          <br>
        </span>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="newHouseSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      modelList: [],
      visible: false,
      newHouse: {
        houseId: 0,
        housePrice: '',
        houseName: '',
        houseAddress: '',
        houseArea: '',
        status: 3,
        houseDesc: '',
        defaultImg: '',
        userId: 0,
        flag: '',
        createTime: ''
      },
      fileList: [],
      dataRule: {
        housePrice: [
          { required: true, message: '金额(/平方)不能为空', trigger: 'blur' }
        ],
        houseName: [
          { required: true, message: '房屋名不能为空', trigger: 'blur' }
        ],
        houseAddress: [
          { required: true, message: '地址不能为空', trigger: 'blur' }
        ],
        houseArea: [
          { required: true, message: '面积(m2)-不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态(0.已卖 1.未卖)不能为空', trigger: 'blur' }
        ],
        houseDesc: [
          { required: true, message: '户型(如1室1厅)不能为空', trigger: 'blur' }
        ],
        defaultImg: [
          { required: true, message: '默认图片不能为空', trigger: 'blur' }
        ],
        userId: [
          { required: true, message: '发布经纪人不能为空', trigger: 'blur' }
        ],
        flag: [
          { required: true, message: '标识(1，新房 2.二手房)不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取模板信息
    getHouseModel (typeId) {
      this.$http({
        url: this.$http.adornUrl(`/house/houseattrkey/info/${typeId}`),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.modelList = data.modelList
        console.log(this.modelList)
      })
    },
    uploadFile (response, file, fileList) {
      console.log(fileList)
      let imgurl = ''
      for (const key in fileList) {
        imgurl = imgurl + ',' + fileList[key].response.url
      }
      this.newHouse.defaultImg = imgurl
      // this.fileList.push({name:'图片',url:response.url})
    },
    handleRemove (file, fileList) {
      console.log(file, fileList)
      let imgurl = ''
      for (const key in fileList) {
        imgurl = imgurl + ',' + fileList[key].response.url
      }
      this.newHouse.defaultImg = imgurl
    },
    handlePreview (file) {
      console.log(file)
    },
    init (id) {
      this.newHouse.houseId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['newHouse'].resetFields()
        if (this.newHouse.houseId) {
          this.$http({
            url: this.$http.adornUrl(`/house/newhouse/info/${this.newHouse.houseId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.newHouse.housePrice = data.newHouse.housePrice
              this.newHouse.houseName = data.newHouse.houseName
              this.newHouse.houseAddress = data.newHouse.houseAddress
              this.newHouse.houseArea = data.newHouse.houseArea
              this.newHouse.status = data.newHouse.status
              this.newHouse.houseDesc = data.newHouse.houseDesc
              this.newHouse.defaultImg = data.newHouse.defaultImg
              this.newHouse.userId = data.newHouse.userId
              this.newHouse.flag = data.newHouse.flag
              this.newHouse.createTime = data.newHouse.createTime
              this.modelList = data.newHouse.attrKeys
            }
          })
        } else {
          this.getHouseModel(1)
        }
      })
    },
    // 表单提交
    newHouseSubmit () {
      this.$refs['newHouse'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/house/newhouse/${!this.newHouse.houseId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'houseId': this.newHouse.houseId || undefined,
              'housePrice': this.newHouse.housePrice,
              'houseName': this.newHouse.houseName,
              'houseAddress': this.newHouse.houseAddress,
              'houseArea': this.newHouse.houseArea,
              'status': this.newHouse.status,
              'houseDesc': this.newHouse.houseDesc,
              'defaultImg': this.newHouse.defaultImg,
              'userId': this.newHouse.userId,
              'flag': this.newHouse.flag,
              'createTime': this.newHouse.createTime,
              'attrKeys': this.modelList
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  },
  created () {
  }
}
</script>
